package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.PhysicalDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.pojo.Physical;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = PhysicalService.class)
@Transactional
public class PhysicalServiceImpl implements PhysicalService{

    @Autowired
    private PhysicalDao physicalDao;

    @Autowired
    private MemberDao memberDao;

    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Map<String,Object>> page = physicalDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Physical physical) {
        physical.setValDate(new Date());
        physicalDao.add(physical);
        String name = physical.getMemberName();
        Integer memberId =memberDao.findByName(name);
        Integer physcalId = physical.getId();
        System.out.println("physicalId = " + physcalId);
        Map<String,Integer> map = new HashMap<>();
        map.put("physcalId",physcalId);
        map.put("memberId",memberId);
        physicalDao.setPhysicalAndMember(map);

    }

    @Override
    public void edit(Physical physical) {
        physicalDao.edit(physical);
    }

    @Override
    public void delete(Integer id) {
        physicalDao.deletePhAndMem(id);
        physicalDao.delete(id);
    }

    @Override
    public Map findById(Integer id) {
        return physicalDao.findById(id);
    }
}
